import json
import random

import database as db

conn = db.conn

if __name__ == '__main__':
    cursor = conn.cursor()

    sql = 'select cve_id from fixes'
    cursor.execute(sql)
    cve_id_list = [cve_id[0] for cve_id in cursor.fetchall()]
    print('全部CVE数量：', len(cve_id_list))
    print('全部CVE名称：', cve_id_list)

    while True:
        idx_list = random.sample(range(0, len(cve_id_list) - 1), 100)

        choose_cve_list = list(set([cve_id_list[idx] for idx in idx_list]))

        choose_cve_list = list(set(choose_cve_list))
        print('挑选出的CVE数量：', len(choose_cve_list))
        print('挑选出的CVE名称：', choose_cve_list)
        if len(choose_cve_list) == 100:
            json_str = json.dumps(choose_cve_list)
            with open('./data.json', 'w', encoding='utf-8') as f:
                f.write(json_str)
            break